﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace NNTH
{
    public partial class fHocVien : DevExpress.XtraEditors.XtraForm
    {
        public fHocVien()
        {
            InitializeComponent();
        }

        Data.QLTTNNTHEntities db = new Data.QLTTNNTHEntities();
        private void fHocVien_Load(object sender, EventArgs e)
        {
            lopHocBindingSource.DataSource = db.LopHoc;
            doiTuongBindingSource.DataSource = db.DoiTuong;
            hocVienBindingSource.DataSource = db.HocVien;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            btnLuu.PerformClick();
            if (gridLookUpEdit1.EditValue != null)
            {
                hocVienBindingSource.AddNew();
            }
            else
            {
                MessageBox.Show("Chọn lớp để thêm học viên và lọc dang sách");
            }
           
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            hocVienBindingSource.RemoveCurrent();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            db.SaveChanges();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void hocVienBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            var Ma = gridLookUpEdit1.EditValue.ToString();
            var stt = db.HocVien.Where(p => p.MaLop == Ma).ToList().Count;
            var hV_tam = db.LopHoc.Where(p => p.MaLop == Ma).First().MaHV +stt.ToString();

            e.NewObject = new Data.HocVien()
            {

                 NgayNop = DateTime.Now,
                 NoiDung ="Thu tiền học phí",
                 NgaySinh= DateTime.Now.AddYears(-15),
                 MaHV = hV_tam, MaLop =Ma,
                 MaDT = db.DoiTuong.First().MaDT
            };
        }

        private void gridLookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (gridLookUpEdit1.EditValue!=null)
            {
                hocVienBindingSource.DataSource = db.HocVien.Where(p => p.MaLop == (string)gridLookUpEdit1.EditValue);
            }
        }

        private void btnInBienLai_Click(object sender, EventArgs e)
        {
            if (this.hocVienBindingSource.Current!=null)
            {
                var report = new Reports.reBienLai();
                BindingSource bs = (BindingSource)report.DataSource;
                bs.DataSource = this.hocVienBindingSource.Current;
                report.ShowPreviewDialog();
            }
        }
    }
}